﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 多开的时候根据宝箱数量来调节高度
/// </summary>
public class AdjustHeight : MonoBehaviour {

    private GridLayoutGroup grid;
    private float height = 0;
    private int num=5;   //每行Cell的个数 
    // Use this for initialization
    public static AdjustHeight _instance;
    void Awake()
    {
        _instance = this;
    }


    public void SetGridHeight()    
    {
        grid = this.GetComponent<GridLayoutGroup>();
        float childCount = this.transform.childCount;  //获得Layout Group子物体个数 
        float temp = childCount %num;
        grid.transform.localPosition = new Vector2(0, -84 - 50 * ((int)childCount / num - 2));
        if (temp != 0)
        {
            height = ((childCount + num) / num) * grid.cellSize.y;  //行数乘以Cell的高度
        }
        else
        {
            height = (childCount / num) * grid.cellSize.y;  //行数乘以Cell的高度
        }
        height += (((childCount + num - 1) / num) - 1) * grid.spacing.y;     //每行之间有间隔  
        grid.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
    }
}
